<%--
  Created by IntelliJ IDEA.
  User: 南苏
  Date: 2022/12/27
  Time: 20:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>ajax请求</title>
    <script type="text/javascript" src="/js/jquery-2.1.1.min.js"></script>
</head>
<body>
    姓名：<input id="name" type="text"><br/>
    性别：<input id="sex" type="text"><br/>
         <input type="button" value="提交" id="btn">


    <script>

        // function commit1() {
        //     var name = $("#name").val();
        //     var sex = $("#sex").val();
        //     $.ajax({
        //         type:"POST",
        //         url:"/student/ajax",
        //         data:{
        //             "name":name,
        //             "sex":sex
        //         },
        //         error:function (request) {
        //             alert("系统错误！")
        //         },
        //         success:function (data) {
        //             console.log(data);
        //         }
        //     })
        // }


        $(function () {
            $("#btn").on('click',function () {
                var name = $("#name").val();
                var sex = $("#sex").val();
                var jsonParam = JSON.stringify({"name":name,"sex":sex});
                $.ajax({
                    type:"POST",
                    url:"/student/ajax",
                    contentType:"application/json",
                    data:jsonParam,
                    // data:{
                    //     "name":name,
                    //     "sex":sex
                    // },
                    error:function (request) {
                        alert("出错啦！")
                    },
                    success:function (data) {
                        console.log(data);
                    }
                })
            })
        })
    </script>

</body>
</html>
